<?php

namespace Admin\Controller;

use Utuo\BackgroundController;
use Utuo\Region;
use Utuo\Phpass;

class AgentController extends BackgroundController {
	
	private $cid = 0;
	
	private $table = array();
	
	// 初始化
	public function _initialize() {
		$this->cid = I("id", 0, "intval");
		$this->setControllerTitle('代理商管理');
		$this->setActionTitle(array(
				'index' => '代理商列表',
				'modify' => '修改代理商', 
		));
		$this->setActionNav(array(
				'index',
		));
	}
	
	// 代理商列表
	public function indexAction() {
		$this->getList(0);
	}
	
	// recycle of customer to display
	public function recycleAction() {
		$this->getList(1);
	}
	
	/**
	 * display the customer list
	 * @param unknown $is_deleted 是否已删除
	 */
	private function getList($is_deleted) {
		/*查询条件*/
		$name = I('name', "");
		/*有条件查询*/
		$condition = array(
			"this0.is_deleted" => $is_deleted
		);
		$param = array();
		$name and $condition['this0.name'] = array('like', "%$name%") and $param['name'] = $name;
		/*搜索*/
		$p = $this->getPage();
		$pageSize = $this->getPageSize();
		$customerListSql = M()->table("__AGENT__ this0")->join('LEFT JOIN __CN_PROVINCE__ this1 on this0.province=this1.code')
							->join('LEFT JOIN __CN_CITY__ this2 on this0.city=this2.code')
							->join('LEFT JOIN __CN_AREA__ this3 on this0.area=this3.code')
							->join('LEFT JOIN __CUSTOMER__ this4 on this0.cid = this4.id')
							->field("this0.*,this1.name as provinceName,this2.name as cityName,this3.name as areaName,this4.name as customerName")
							->select(false);
		$agentLists = M()->table($customerListSql." this0")->where($condition)->limit($pageSize)->page($p)->order('id')->select();
		$count = M()->table($customerListSql." this0")->where($condition)->count();
		$this->assign("agentLists", $agentLists);
		$this->assign("param", $param);
		$this->showPage($count, $pageSize, $param);
		$this->display();
	}
	
	// redirect to modiy page
	public function modifyAction() {
		$agent = $this->getOneById("Agent");
		$partnerLists = M("Agent")->where('is_deleted=0')->getField('id,name');
		$agent['provinceOptions'] = $this->getOption(Region::getProvinceList(), $agent['province'], '==请选择省份==');
		$agent['cityOptions'] = $this->getOption(Region::getCityListByProvince($agent['province']), $agent['city'], '==请选择城市==');
		$agent['areaOptions'] = $this->getOption(Region::getAreaListByCity($agent['city']), $agent['area'], '==请选择地区==');
		$this->assign("agent", $agent);
		$this->display();
	}
	
	// 超级登录
	public function loginAction() {
	    $adminId = I("id", 0, "intval");
	    $agentId = I("agentId", 0, "intval");
	    $agentId and $adminId = M("AgentAdmin")->where('agent_id=%d and is_super=1', $agentId)->getField('id');
	    $admin = M("AgentAdmin a")->join("__AGENT__ b on a.agent_id=b.id", "LEFT")->where("a.id={$adminId}")->field('a.*,b.cid')->find();
	    $agentId = $agentId ? $agentId : $admin['cid'];
	    $_SESSION["_agent_"] = array(
	        "userid" => $admin['id'],
	        "id" => $agentId,
	        "cid" => $admin['cid'],
	        "name" => $admin['name']."(".session("name").")",
	        "agentManagerConfig" => null,
	        self::LOGIN_TIME => time(),
	        self::LAST_ACTIVE => time(),
	        self::IS_LOGINED => true,
	    );
	    $_SESSION["_agent_"]["sessionKeys"] = serialize( array_keys($_SESSION["_agent_"] ) );
	    redirect(U("Agent/Index/index"), 0);
	}
	
	// modify customer and execute
	public function modifyPostAction() {
		$d = D("Agent");
		if ( $d->create() ) {
			if (false!==$d->save()) {
				$this->ajaxSuccess();
			} else {
				$this->ajaxError();
			}
		} else {
			$this->ajaxError($d->getError());
		}
	}
	
	// 获取城市
	public function getCityPostAction() {
		echo $this->getOption(Region::getCityListByProvince(I('code')), '', '==请选择城市==');
	}
	
	// 获取地区
	public function getAreaPostAction() {
		echo $this->getOption(Region::getAreaListByCity(I('code')), '', '==请选择地区==');
	}
	
}